package pl.vm.domain;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import pl.vm.domain.interfaces.Classification;
import pl.vm.domain.interfaces.RoundManager;
import pl.vm.domain.interfaces.SeazonLoopback;
import pl.vm.domain.interfaces.TeamManager;
import pl.vm.domain.interfaces.TransferManager;

/**
 * Class that is entry point of application. It contains all rounds, statistics and classifications
 * 
 * @author krzysztof.graczyk
 *
 */
public class Seazon {

	/* e.g. 2011/2012 */
	private String id;
	private Map<Player, Double> allPoints = new HashMap<>();
	private Set<Player> allPlayers = new HashSet<Player>();
	private Classification classification;
	private TransferManager transferManager;
	private RoundManager roundManager;
	private TeamManager teamManager;
				
	/**
	 * Creates new seazon. Seazon id should be something like the example: 2011/2012
	 * @param id
	 */
	public Seazon(String id) {
		this.id = id;
		this.roundManager = new RoundManagerImpl(this);
		this.classification = new SeazonClassification(this);
		this.teamManager = new TeamManagerImpl(this);
	}
			
	public RoundManager getRoundManager() {
		return this.roundManager;
	}
	
	public TeamManager getTeamManager() {
		return this.teamManager;
	}
	
	public TransferManager getTransferManager() {
		return this.transferManager;
	}
	
	/**
	 * Returns all points that player made in VM from all rounds
	 * @param player
	 * @return number of points
	 */
	public Double getAllPointsForPlayer(Player player) {
		return allPoints.get(player);
	}
	
	public String getId() {
		return this.id;
	}
	
	public Classification getClassification() {
		return this.classification;
	}
	
	public Set<Player> getAllPlayers() {
		return this.allPlayers;
	}
	
	class SeazonLoopbackImpl implements SeazonLoopback {

		@Override
		public void addPointsForPlayer(Player player, double points) {
			double newPoints = 0;
			if (!allPoints.containsKey(player)) {
				allPoints.put(player, points);
				return;
			}
			newPoints = allPoints.get(player) + points;
			allPoints.put(player, newPoints);
		}

		@Override
		public void removePointsForPlayer(Player player, double points) {
			if (!allPoints.containsKey(player)) {
				throw new RuntimeException("Player does not exist!, player " + player.toString());
			}
			double newPoints = allPoints.get(player) - points;
			allPoints.put(player, newPoints);
		}

		@Override
		public String getSeazonId() {
			return id;
		}
	}
}
